GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#2843)
by Brendan
04:11
created

$.fn.symphonyDrawer   C

Complexity

Conditions 9
Paths 48

Size

Total Lines 76

Duplication

Lines 75
Ratio 98.68 %

Importance

Changes 0
Metric Value
cc 9
c 0
b 0
f 0
nc 48
nop 3
dl 75
loc 76
rs 5.8219

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * @package assets
3
 */
4
5
(function($, Symphony) {
6
7
	/**
8
	 * Drawers are hidden areas in the backend that are used to
9
	 * display additional content on request. There are three different
10
	 * types of drawers: horizontal, vertical left and vertical right.
11
	 *
12
	 * @name $.symphonyDrawer
13
	 * @class
14
	 *
15
	 * @param {Object} options An object specifying containing the attributes specified below
16
	 * @param {Integer} [options.verticalWidth=300] Width of the vertical drawers
17
	 * @param {String} [options.speed='fast'] Animation speed
18
	 *
19
	 * @example
20
21
			$('.drawer').symphonyDrawer();
22
	 */
23
	$.fn.symphonyDrawer = function(options) {
24
		var objects = this,
25
			wrapper = $('#wrapper'),
26
			contents = $('#contents'),
27
			form = contents.find('> form'),
28
			settings = {
29
				verticalWidth: 300,
30
				speed: 'fast'
31
			};
32
33
		$.extend(settings, options);
34
35
	/*-------------------------------------------------------------------------
36
		Events
37
	-------------------------------------------------------------------------*/
38
39
		// Expand drawer
40
		objects.on('expand.drawer', function expand(event, speed, stay) {
41 View Code Duplication
			var drawer = $(this),
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
42
				position = drawer.data('position'),
43
				buttons = $('.button.drawer'),
44
				button = buttons.filter('[href="#' + drawer.attr('id') + '"]'),
45
				samePositionButtons = buttons.filter('.' + position),
46
				context = drawer.data('context') ? '.' + drawer.data('context') : '',
47
				height = getHeight();
48
49
			drawer.trigger('expandstart.drawer');
50
51
			speed = (typeof speed === 'undefined' ? settings.speed : speed);
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter speed. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
52
			stay = (typeof stay === 'undefined' ? false : true);
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter stay. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
Unused Code introduced by
The assignment to variable stay seems to be never used. Consider removing it.
Loading history...
53
54
			// update button state
55
			samePositionButtons.removeClass('selected');
56
57
			// Close opened drawers from same region
58
			$('.drawer.' + position).filter(function(index) {
0 ignored issues
show
Unused Code introduced by
The parameter index is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
59
				return $(this).data('open');
60
			}).trigger('collapse.drawer', [speed, true]);
61
62
			if (position === 'vertical-left') {
63
				drawer.css({
64
					width: 0,
65
					height: height,
66
					display: 'block'
67
				})
68
				.animate({
69
					width: settings.verticalWidth
70
				}, {
71
					duration: speed,
72
					step: function(now, fx){
0 ignored issues
show
Unused Code introduced by
The parameter fx is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
73
						form.css('margin-left', now + 1); // +1px right border
74
					},
75
					complete: function() {
76
						form.css('margin-left', settings.verticalWidth + 1); // +1px right border
77
						drawer.trigger('expandstop.drawer');
78
					}
79
				});
80
			}
81
			else if (position === 'vertical-right') {
82
				drawer.css({
83
					width: 0,
84
					height: height,
85
					display: 'block'
86
				})
87
				.animate({
88
					width: settings.verticalWidth
89
				}, {
90
					duration: speed,
91
					step: function(now, fx){
0 ignored issues
show
Unused Code introduced by
The parameter fx is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
92
						form.css('margin-right', now + 1); // +1px left border
93
					},
94
					complete: function() {
95
						form.css('margin-right', settings.verticalWidth + 1); // +1px right border
96
						drawer.trigger('expandstop.drawer');
97
					}
98
				});
99
			}
100
			else if (position === 'horizontal') {
101
				drawer.animate({
102
					height: 'show'
103
				}, {
104
					duration: speed,
105
					complete: function() {
106
						drawer.trigger('expandstop.drawer');
107
					}
108
				});
109
			}
110
111
			button.addClass('selected');
112
113
			// store state
114
			if(Symphony.Support.localStorage === true) {
115
				// Put in a try/catch incase we exceed storage space
116
				try {
117
					window.localStorage['symphony.drawer.' + drawer.attr('id') + context] = 'opened';
118
				}
119
				catch(e) {
120
					window.onerror(e.message);
121
				}
122
			}
123
124
			wrapper.addClass('drawer-' + position);
125
			drawer.data('open', true);
126
		});
127
128
		// Collapse drawer
129
		objects.on('collapse.drawer', function collapse(event, speed, stay) {
130 View Code Duplication
			var drawer = $(this),
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
131
				position = drawer.data('position'),
132
				buttons = $('.button.drawer'),
133
				button = buttons.filter('[href="#' + drawer.attr('id') + '"]'),
134
				context = drawer.data('context') ? '.' + drawer.data('context') : '';
135
136
			drawer.trigger('collapsestart.drawer');
137
138
			speed = (typeof speed === 'undefined' ? settings.speed : speed);
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter speed. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
139
			stay = (typeof stay === 'undefined' ? false : true);
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter stay. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
140
141
			// update button state
142
			button.removeClass('selected');
143
144
			if (position === 'vertical-left') {
145
				drawer.animate({
146
					width: 0
147
				}, {
148
					duration: speed,
149
					step: function(now, fx){
0 ignored issues
show
Unused Code introduced by
The parameter fx is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
150
						if (!stay) {
151
							form.css('margin-left', now);
152
						}
153
					},
154
					complete: function() {
155
						drawer.css({
156
							display: 'none'
157
						})
158
						.trigger('collapsestop.drawer');
159
					}
160
				});
161
			}
162
			else if (position === 'vertical-right') {
163
				drawer.animate({
164
					width: 0
165
				}, {
166
					duration: speed,
167
					step: function(now, fx){
0 ignored issues
show
Unused Code introduced by
The parameter fx is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
168
						if (!stay) {
169
							form.css('margin-right', now);
170
						}
171
					},
172
					complete: function() {
173
						drawer.css({
174
							display: 'none'
175
						})
176
						.trigger('collapsestop.drawer');
177
					}
178
				});
179
			}
180
			else if (position === 'horizontal') {
181
				drawer.animate({
182
					height: 'hide'
183
				}, {
184
					duration: speed,
185
					complete: function() {
186
						drawer.trigger('collapsestop.drawer');
187
					}
188
				});
189
			}
190
191
			// store state
192
			if(Symphony.Support.localStorage === true) {
193
				// Put in a try/catch incase we exceed storage space
194
				try {
195
					window.localStorage['symphony.drawer.' + drawer.attr('id') + context] = 'closed';
196
				}
197
				catch(e) {
198
					window.onerror(e.message);
199
				}
200
			}
201
202
			wrapper.removeClass('drawer-' + position);
203
			drawer.data('open', false);
204
		});
205
206
		// Resize drawers
207
		$(window).on('resize.drawer load.drawer', function() {
208
			var height = getHeight();
209
			objects.filter('.vertical-left, .vertical-right').css('height', height);
210
		});
211
212
	/*-------------------------------------------------------------------------
213
		Utilities
214
	-------------------------------------------------------------------------*/
215
216
		var getHeight = function() {
217
			var height = Math.max(window.innerHeight - contents[0].offsetTop - 1, contents[0].clientHeight);
218
219
			return height;
220
		};
221
222
	/*-------------------------------------------------------------------------
223
		Initialisation
224
	-------------------------------------------------------------------------*/
225
226
		objects.each(function drawers() {
227
			var drawer = $(this),
228
				position = drawer.data('position'),
0 ignored issues
show
Unused Code introduced by
The variable position seems to be never used. Consider removing it.
Loading history...
229
				button = $('.button.drawer[href="#' + drawer.attr('id') + '"]'),
230
				context = drawer.data('context') ? '.' + drawer.data('context') : '',
231
				storedState;
232
233
			// Initial state
234
			if (drawer.data('default-state') === 'opened') {
235
				drawer.data('open', true);
236
			}
237
			// Restore state
238
			if (Symphony.Support.localStorage === true) {
239
				storedState = window.localStorage['symphony.drawer.' + drawer.attr('id') + context];
240
				if (storedState === 'opened') {
241
					drawer.data('open', true);
242
				}
243
				else if (storedState === 'closed') {
244
					drawer.data('open', false);
245
				}
246
			}
247
248
			// Click event for the related button
249
			button.on('click.drawer', function(event) {
250
				event.preventDefault();
251
				!drawer.data('open') ? drawer.trigger('expand.drawer') : drawer.trigger('collapse.drawer');
252
			});
253
254
			// Initially opened drawers
255
			drawer.data('open') ? drawer.trigger('expand.drawer', [0]) : drawer.trigger('collapse.drawer', [0, true]);
256
		});
257
258
	/*-----------------------------------------------------------------------*/
259
260
		return objects;
261
	};
262
263
})(window.jQuery, window.Symphony);
264